package vip.specimen.system.service;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import vip.specimen.system.datasource.entity.SystemApiConfigEntity;
import vip.specimen.system.repository.SystemApiConfigRepository;
import vip.specimen.system.utils.CommonUtil;
import vip.specimen.system.utils.DataBaseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/02/19 16:30
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Service
@Slf4j
public class SystemApiConfigService {
    @Resource
    private SystemApiConfigRepository apiRepository;
    @Resource
    private HttpServletRequest request;

    /**
     * 添加api
     *
     * @return 数据
     */
    public SystemApiConfigEntity insert() {
        SystemApiConfigEntity api = null;
        String[] accessKeyList = UUID.randomUUID().toString().split("-");
        try {
            api = new SystemApiConfigEntity(accessKeyList[0] + accessKeyList[accessKeyList.length - 1], UUID.randomUUID().toString().replaceAll("-", ""));
            this.apiRepository.save(api);
        } catch (Exception e) {
            log.error("创建API异常:" + e.getMessage());
            api = null;
        }
        return api;

    }

    /**
     * 更新API  key
     *
     * @param uuid UUID参数
     * @return
     */
    public SystemApiConfigEntity update(UUID uuid) {
        String[] accessKeyList = UUID.randomUUID().toString().split("-");
        try {
            if (this.apiRepository.existsById(uuid)) {
                SystemApiConfigEntity api = this.apiRepository.findById(uuid).get();
                api.setAccessKey(accessKeyList[0] + accessKeyList[accessKeyList.length - 1]);
                api.setSecretKey(UUID.randomUUID().toString());
                api.setUpdateTime(Calendar.getInstance().getTime());
                this.apiRepository.saveAndFlush(api);
                return api;
            }
        } catch (Exception e) {
            log.error("更新API异常:" + e.getMessage());
        }
        return null;
    }

    /**
     * 根据uuid删除
     *
     * @param uuid ID
     * @return
     */
    public boolean delete(UUID uuid) {
        try {
            if (this.apiRepository.existsById(uuid)) {
                this.apiRepository.deleteById(uuid);
                return true;
            }
        } catch (Exception e) {
            log.error("删除API异常:" + e.getMessage());
        }
        return false;
    }

    /**
     * 查询一条数据
     *
     * @param uuid ID
     * @return
     */
    public SystemApiConfigEntity findOne(UUID uuid) {
        if (this.apiRepository.existsById(uuid)) {
            return this.apiRepository.findById(uuid).get();
        }
        return null;
    }

    /**
     * 启用-停用  更新切换
     *
     * @param uuid ID
     * @return 数据
     */
    public SystemApiConfigEntity updateEnable(UUID uuid) {
        SystemApiConfigEntity api = null;
        try {
            if (this.apiRepository.existsById(uuid)) {
                api = this.apiRepository.findById(uuid).get();
                api.setEnable(!api.isEnable());
                api.setUpdateTime(Calendar.getInstance().getTime());
                this.apiRepository.save(api);
            }
        } catch (Exception e) {
            log.error("更新API异常:" + e.getMessage());
            api = null;
        }
        return api;
    }


    public Page<SystemApiConfigEntity> findAll() {
        JSONObject param = CommonUtil.pageParams(this.request);
        Pageable pageable = DataBaseUtil.getPageable(param.getIntValue("page"), param.getIntValue("pageSize"), DataBaseUtil.getSort("desc", "createTime"));
        return this.apiRepository.findAll(pageable);
    }
}
